<?php
namespace app\controller;
use app\BaseController;
use think\App;
use think\facade\View;
use think\response\Json;

class Base extends BaseController {

    protected static $name;
    protected  static $sex;

    protected static $view;
    // 构造函数
    public function __construct(App $app = null) {
        date_default_timezone_set('PRC');
        parent::__construct($app);
        self::$view=new View();
    }

    /**
     * 返回JSON格式的API响应
     */
    protected function Success($data = [], $code = 200, $msg = 'success') {
        $src=([
            'code' => $code,
            'message' => $msg,
            'data' => $data
        ]);
       // echo json_encode($src);
        return \json($src);
    }

    /**
     * 返回错误信息的API响应
     */
    protected function Error($errorMsg = '', $code = 500) {
        $data=([
            'code' => $code,
            'message' => $errorMsg ?: 'Internal Server Error',
            'data' => []
        ]);
        return \json($data);
    }

    protected function viewSuccess($url = '', $src  = '') {
        View::assign('src',$src);
        View::assign('url',$url);
        return View::fetch('/user/transfer');
    }

    protected function viewError($url = '', $src  = '') {
        View::assign('src',$src);
        View::assign('url',$url);
        return View::fetch('/user/transfer');
    }
}